
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/highsea/index',
                    add_url: 'business/highsea/add',
                    del_url: 'business/highsea/del',
                    table: 'business',
                },
                pageSize: 20, //调整分页大小为20
                pageList: [10, 25, 50, 100, 'All'], //增加一个100的分页大小
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                // 当访问列表发起ajax请求数据
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                // 主键
                pk: 'id',
                // 设置默认排序
                sortName: 'id',
                sortOrder: 'desc',
                search: false, // 取消快速搜索框
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'scource.name', title: __('SourceName'), operate: 'LIKE',searchList:$.getJSON("business/highsea/source") },
                        { field: 'gender', title: __('Gender'), formatter: Table.api.formatter.normal, searchList: { "0": __('保密'), "1": __('男'), "2": __('女') } },
                        { field: 'deal', title: __('Deal'), formatter: Table.api.formatter.normal, searchList: { "0": __('未成交'), "1": __('已成交') } },
                        { field: 'auth', title: __('Auth'), searchList: { "0": __('未认证'), "1": __('已认证') }, formatter: Table.api.formatter.normal },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'apply',
                                    icon: 'fa fa-arrow-down',
                                    confirm: '确定要领取吗',
                                    title: '领取',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    url: 'business/highsea/apply?ids={id}',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                                {
                                    name: 'recovery',
                                    title: '分配',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-success btn-xs btn-dialog',
                                    icon: 'fa fa-arrows-h',
                                    url: 'business/highsea/recovery?ids={id}',
                                }
                            ]
                        }
                    ]
                ]
            })
            $('.apply').click(function () {
                $ids = Table.api.selectedids(table)
                layer.confirm('确定领取' + $ids.length + "个客户吗?", { icon: 3, title: '提示' }, function (index) {
                    if (index) {
                        Backend.api.ajax("business/highsea/apply?ids=" + $ids.join(","), function (response) {
                            // 处理返回的结果
                            layer.close(index);
                            // 刷新数据表格
                            table.bootstrapTable('refresh')
                        })

                    }
                    layer.close(index);
                });
            })
            $('.recovery').click(function(){
                $ids = Table.api.selectedids(table)
                Backend.api.open('business/highsea/recovery?ids='+$ids.join(","), '分配')
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        // 添加
        add: function () {
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
        recovery:function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    }

    return Controller;
});